package joc.contract;

import joc.customize.Contract;
import joc.customize.ForBehaviorCallAtBeginningOfBehavior;
import joc.customize.ForBehaviorEvaluateAtEndOfBehavior;
import joc.customize.PartialEvaluationResult;
import joc.customize.StateRetainingContract;

public class ProfileContract extends Contract<Profile> implements StateRetainingContract {
	private long startTime;

	@ForBehaviorCallAtBeginningOfBehavior
	public void initContract(Profile annotation) {
		startTime = System.nanoTime();
	}

	@ForBehaviorEvaluateAtEndOfBehavior
	public PartialEvaluationResult checkContract(Profile annotation) {
		long nanoValue = annotation.value();
		String errorValue = "" + annotation.value();
		if (!annotation.nano()) {
			nanoValue *= 1000000;
			errorValue += " micro-seconds";
		} else {
			errorValue += " nano-seconds";
		}
		return getResult(System.nanoTime() - startTime < nanoValue, "Method execution took longer than " + errorValue
				+ ".");
	}
}
